//
//  AlbumPreviewVC.m
//  PhotoKitDemo
//
//  Created by 红沙尘 on 17/5/3.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "AlbumPreviewVC.h"
#import "AlbumCell.h"


@interface AlbumPreviewVC ()
{
    CGSize itemSize;
}
@end

@implementation AlbumPreviewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    itemSize = myCollectionView.bounds.size;
    
    [myCollectionView registerNib:[UINib nibWithNibName:@"AlbumCell" bundle:nil]
       forCellWithReuseIdentifier:@"AlbumCell"];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing      = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset            = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.itemSize                = itemSize;
    flowLayout.scrollDirection         = UICollectionViewScrollDirectionHorizontal;
    
    [myCollectionView setCollectionViewLayout:flowLayout];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return self.dataSource.assets.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    AlbumCell *cell = (AlbumCell *)[cv dequeueReusableCellWithReuseIdentifier:@"AlbumCell"
                                                                 forIndexPath:indexPath];
    
    CGSizeMake(self.dataSource.assets[indexPath.row].pixelWidth, self.dataSource.assets[indexPath.row].pixelHeight);
    [[PHCachingImageManager defaultManager] requestImageForAsset:self.dataSource.assets[indexPath.row]
                                                      targetSize:CGSizeMake(itemSize.width*2, itemSize.height*2)
                                                     contentMode:PHImageContentModeAspectFill
                                                         options:self.dataSource.options
                                                   resultHandler:^(UIImage * _Nullable result,
                                                                   NSDictionary * _Nullable info)
     {
         cell.photoImageView.image = result;
     }];
    
    cell.backgroundColor = [UIColor colorWithRed:(arc4random()%100 + 155)/255.0f
                                           green:(arc4random()%100 + 155)/255.0f
                                            blue:(arc4random()%100 + 155)/255.0f
                                           alpha:1];
    
    return cell;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
